<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- tutorial.qdoc -->
  <title>QML Tutorial 3 - States and Transitions | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >QML 教程 3 - 状态和转换</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qml-tutorial2.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-tutorial2.html">QML 教程 2 - QML 组件</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#walkthrough">Walkthrough</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML 教程 3 - 状态和转换</h1>
<span class="subtitle"></span>
<!-- $$$qml-tutorial3.html-description -->
<div class="descr"> <a name="details"></a>
<p>在本章中，我们通过引入状态和转换使这个例子更加动态。</p>
<p>我们希望我们的文本移动到屏幕底部，旋转并在单击时变为红色。</p>
<p class="centerAlign"><img src="images/declarative-tutorial3_animation.gif" alt="" /></p><p>这是 QML 代码：</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">page</span>
      <span class="name">width</span>: <span class="number">320</span>; <span class="name">height</span>: <span class="number">480</span>
      <span class="name">color</span>: <span class="string">&quot;lightgray&quot;</span>

      <span class="type"><a href="qml-qtquick-text.html">Text</a></span> {
          <span class="name">id</span>: <span class="name">helloText</span>
          <span class="name">text</span>: <span class="string">&quot;Hello world!&quot;</span>
          <span class="name">y</span>: <span class="number">30</span>
          <span class="name">anchors</span>.horizontalCenter: <span class="name">page</span>.<span class="name">horizontalCenter</span>
          <span class="name">font</span>.pointSize: <span class="number">24</span>; <span class="name">font</span>.bold: <span class="number">true</span>

          <span class="type"><a href="qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">id</span>: <span class="name">mouseArea</span>; <span class="name">anchors</span>.fill: <span class="name">parent</span> }

          <span class="name">states</span>: <span class="name">State</span> {
              <span class="name">name</span>: <span class="string">&quot;down&quot;</span>; <span class="name">when</span>: <span class="name">mouseArea</span>.<span class="name">pressed</span> <span class="operator">==</span> <span class="number">true</span>
              <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">helloText</span>; <span class="name">y</span>: <span class="number">160</span>; <span class="name">rotation</span>: <span class="number">180</span>; <span class="name">color</span>: <span class="string">&quot;red&quot;</span> }
          }

          <span class="name">transitions</span>: <span class="name">Transition</span> {
              <span class="name">from</span>: <span class="string">&quot;&quot;</span>; <span class="name">to</span>: <span class="string">&quot;down&quot;</span>; <span class="name">reversible</span>: <span class="number">true</span>
              <span class="type"><a href="qml-qtquick-parallelanimation.html">ParallelAnimation</a></span> {
                  <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">properties</span>: <span class="string">&quot;y,rotation&quot;</span>; <span class="name">duration</span>: <span class="number">500</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
                  <span class="type"><a href="qml-qtquick-coloranimation.html">ColorAnimation</a></span> { <span class="name">duration</span>: <span class="number">500</span> }
              }
          }
      }

      <span class="type"><a href="qml-qtquick-grid.html">Grid</a></span> {
          <span class="name">id</span>: <span class="name">colorPicker</span>
          <span class="name">x</span>: <span class="number">4</span>; <span class="name">anchors</span>.bottom: <span class="name">page</span>.<span class="name">bottom</span>; <span class="name">anchors</span>.bottomMargin: <span class="number">4</span>
          <span class="name">rows</span>: <span class="number">2</span>; <span class="name">columns</span>: <span class="number">3</span>; <span class="name">spacing</span>: <span class="number">3</span>

          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;red&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;green&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;blue&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;yellow&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;steelblue&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
          <span class="type">Cell</span> { <span class="name">cellColor</span>: <span class="string">&quot;black&quot;</span>; <span class="name">onClicked</span>: <span class="name">helloText</span>.<span class="name">color</span> <span class="operator">=</span> <span class="name">cellColor</span> }
      }
  }

</pre>
<a name="walkthrough"></a>
<h2 id="walkthrough">演练</h2>
<pre class="qml">

          <span class="name">states</span>: <span class="name">State</span> {
              <span class="name">name</span>: <span class="string">&quot;down&quot;</span>; <span class="name">when</span>: <span class="name">mouseArea</span>.<span class="name">pressed</span> <span class="operator">==</span> <span class="number">true</span>
              <span class="type"><a href="qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">helloText</span>; <span class="name">y</span>: <span class="number">160</span>; <span class="name">rotation</span>: <span class="number">180</span>; <span class="name">color</span>: <span class="string">&quot;red&quot;</span> }
          }

</pre>
<p>首先，我们为文本类型创建一个新的<i>down</i> 状态。此状态将在按下<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-mousearea.html">MouseArea</a>时激活，并在释放时停用。</p>
<p><i>down</i> 状态包括一组从我们的隐式<i>默认状态</i>（最初在 QML 中定义的项目）的属性更改。具体来说，我们将<code>y</code>文本的属性设置为<code>160</code>，旋转为<code>180</code>，<code>color</code>为红色。</p>
<pre class="qml">

          <span class="name">transitions</span>: <span class="name">Transition</span> {
              <span class="name">from</span>: <span class="string">&quot;&quot;</span>; <span class="name">to</span>: <span class="string">&quot;down&quot;</span>; <span class="name">reversible</span>: <span class="number">true</span>
              <span class="type"><a href="qml-qtquick-parallelanimation.html">ParallelAnimation</a></span> {
                  <span class="type"><a href="qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">properties</span>: <span class="string">&quot;y,rotation&quot;</span>; <span class="name">duration</span>: <span class="number">500</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
                  <span class="type"><a href="qml-qtquick-coloranimation.html">ColorAnimation</a></span> { <span class="name">duration</span>: <span class="number">500</span> }
              }
          }

</pre>
<p>因为我们不希望文本立即出现在底部，而是要平滑移动，所以我们在两个状态之间添加了一个过渡。</p>
<p><code>from</code>并<code>to</code>定义转换将运行的状态。在这种情况下，我们希望从默认状态转换到我们的<i>down</i> 状态。</p>
<p>因为我们希望相同的转换在从<i>down</i> 状态切换到默认状态时反向运行，所以我们将<code>reversible</code> 设置为<code>true</code>。这相当于分别编写两个转换。</p>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-parallelanimation.html">ParallelAnimation</a>类型确保两种类型的动画（数字和颜色）同时开始。我们也可以使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-sequentialanimation.html">SequentialAnimation</a>一个接一个地运行它们。</p>
<p>有关状态和转换的更多详细信息，请参阅<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-statesanimations-states.html">Qt Quick States</a>和<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtwidgets/qtwidgets-animation-states-example.html">状态和转换示例</a>。</p>
</div>
<!-- @@@qml-tutorial3.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qml-tutorial2.html">QML Tutorial 2 - QML Components</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
